using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (User.Identity.IsAuthenticated)
        {
            createSession();
        }
       
    }
    protected void createSession()
    {
        SqlConnection conn;
        SqlCommand comm;
        SqlDataReader reader;
        string username = User.Identity.Name.ToUpper().Trim().ToString();

        string connectionString = ConfigurationManager.ConnectionStrings["Capstone"].ConnectionString;

        conn = new SqlConnection(connectionString);

        string studentQuery = @"SELECT student_ID AS 'ID',
                                    first_Name AS 'FIRST',
                                    last_Name AS 'LAST'
                                FROM
                                    Student
                                WHERE
                                    username = @username";

        string instructorQuery = @"SELECT instructor_ID AS 'ID',
                                    first_Name AS 'FIRST',
                                    last_Name AS 'LAST'
                                FROM
                                    Instructor
                                WHERE
                                    username = @username";

        string adminQuery = @"SELECT admin_ID AS 'ID',
                                    first_Name AS 'FIRST',
                                    last_Name AS 'LAST'
                                FROM
                                    Admin
                                WHERE
                                    username = @username";

        if(User.IsInRole("Administrator"))
        {
            comm = new SqlCommand(adminQuery, conn);
        }
        else if (User.IsInRole("Instructor"))
        {
            comm = new SqlCommand(instructorQuery, conn);
        }
        else
        {
            comm = new SqlCommand(studentQuery, conn);
        }
        conn.Open();
        comm.Parameters.AddWithValue("@username", username);
        reader = comm.ExecuteReader();

        while (reader.Read())
        {
            Session["ID"] = reader["ID"].ToString();
            Session["FIRST"] = reader["FIRST"].ToString();
            Session["LAST"] = reader["LAST"].ToString();
        }
        conn.Close();

    }
}
